<template>
	<view>
		<view class="container">
			<view style="height: 160rpx;" class="bg-gradual-orange2">
				<view style="height: 160rpx;width: 100%;display: flex;">
					<view style="height: 60rpx;width: 100%; margin-top: 110rpx;font-size: 32rpx;display: flex;">
						<view style="width: 20%;text-align: center;line-height: 32rpx;" @click="back()">
							<text class="cuIcon-back" style="height: 60rpx;"></text>
							<text> 返回</text>
						</view>
						<view style="width: 56%;text-align: center;line-height: 32rpx;">
							<text>收货地址</text>
						</view>
					</view>
				</view>
			</view>
			<AddressList fromPage="list" :change-list-time="changeListTime" @edit="editAddress"></AddressList>
		    <view class='footer acea-row row-between-wrapper'>
				<view class='addressBnt bg-color'  @click="addAddress"><text class='iconfont icon-tianjiadizhi'></text>添加新地址</view>
				<view class='addressBnt wxbnt' @click="getWxAddress"><text class='iconfont icon-weixin2'></text>导入微信地址</view>
		    </view>
		</view>
	</view>
</template>

<script>
	import AddressList from '../../components/address/list.vue'
	import { editAddress } from '@/api/address.js'
	export default {
		components: {
			AddressList
		},
		data() {
			return {
				changeListTime: ''
			}
		},
		onLoad() {
		},
		onShow() {
			this.changeListTime = new Date()
		},
		onReady() {},
		methods: {
			/**返回*/
			back() {
				uni.navigateBack({
					delta: 1
				})
			},
			// 添加地址
			addAddress() {
				console.log(1234)
				uni.navigateTo({
					url: './add-address'
				})
			},
			editAddress(id) {
				uni.navigateTo({
					url: './edit-address?id=' + id
				})
			},
			// 导入共享地址（小程序）
			getWxAddress: function() {
				let that = this;
				uni.authorize({
					scope: 'scope.address',
					success: function(res) {
						uni.chooseAddress({
							success: function(res) {
								that.userAddress = {
									consignee: res.userName,
									phone: res.telNumber,
									provinceName: res.provinceName,
									cityName: res.cityName,
									areaName: res.countyName,
									address: res.detailInfo
								}
								editAddress(that.userAddress, '/users/address/add').then(res=>{
									if (res.code == 200) {
										that.changeListTime = new Date()
									} else {
										uni.showToast({
											title: res.msg,
											duration: 1000,
											icon: 'none',
										});
									}
								})
							},
							fail: function(res) {
								if (res.errMsg == 'chooseAddress:cancel') return that.$util
									.Tips({
										title: '取消选择'
									});
							},
						})
					},
					fail: function(res) {
						uni.showModal({
							title: '您已拒绝导入微信地址权限',
							content: '是否进入权限管理，调整授权？',
							success(res) {
								if (res.confirm) {
									uni.openSetting({
										success: function(res) {}
									});
								} else if (res.cancel) {
									return that.$util.Tips({
										title: '已取消！'
									});
								}
							}
						})
					},
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	.bg-gradual-orange2 {
		background-image: linear-gradient(45deg, #FF707A, #FD505C);
		color: #ffffff;
	}

	.line-orange2 {
		color: #FF707A;
	}
	.container .footer{position:fixed;width:100%;background-color:#fff;bottom:0;height:106rpx;padding:0 30rpx;box-sizing:border-box;}
	.container .footer .addressBnt{width:330rpx;height:76rpx;border-radius:50rpx;text-align:center;line-height:76rpx;font-size:30rpx;color:#fff;}
	.container .footer .addressBnt .iconfont{font-size:35rpx;margin-right:8rpx;vertical-align:-1rpx;}
	.container .footer .addressBnt.wxbnt{background-color:#fe960f;}
	.bg-color{background-color:#e93323!important;}
</style>